package com.hertz.iris;

import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.FROYO;

import org.acra.ACRA;
import org.acra.ReportField;
import org.acra.ReportingInteractionMode;
import org.acra.annotation.ReportsCrashes;

import android.app.Application;
import android.app.Instrumentation;
import android.content.Context;
import android.graphics.Typeface;

import com.github.kevinsawicki.http.HttpRequest;
import com.hertz.iris.dao.DaoMaster;
import com.hertz.iris.dao.DaoMaster.OpenHelper;
import com.hertz.iris.dao.DaoSession;
import com.hertz.iris.util.Constants;

/**
 * alarm application
 */
@ReportsCrashes(formKey = "", // will not be used
formUri = Constants.Http.URL_ERRORLOG,
reportType = org.acra.sender.HttpSender.Type.JSON,
mode = ReportingInteractionMode.TOAST,
applicationLogFile="/applog.log",
applicationLogFileLines=300,
customReportContent={ReportField.ANDROID_VERSION,ReportField.PHONE_MODEL,ReportField.APP_VERSION_CODE,ReportField.APP_VERSION_NAME,ReportField.STACK_TRACE},
resToastText = R.string.program_error)
public class IrisApplication extends Application {

    private static IrisApplication instance;
    private Typeface font;
    private DaoMaster daoMaster;  
    private DaoSession daoSession;  
    /**
     * Create main application
     */
    public IrisApplication() {
        // Disable http.keepAlive on Froyo and below
        if (SDK_INT <= FROYO)
            HttpRequest.keepAlive(false);
    }

    /**
     * Create main application
     *
     * @param context
     */
    public IrisApplication(final Context context) {
        this();
        attachBaseContext(context);

    }

    @Override
    public void onCreate() {
        super.onCreate();

        instance = this;

        // Perform injection
        Injector.init(getRootModule(), this);
        font = Typeface.createFromAsset(getAssets(), "fonts/fontawesome-webfont.ttf");
        ACRA.init(this);

    }

    private Object getRootModule() {
        return new RootModule();
    }


    /**
     * Create main application
     *
     * @param instrumentation
     */
    public IrisApplication(final Instrumentation instrumentation) {
        this();
        attachBaseContext(instrumentation.getTargetContext());
    }

    public static IrisApplication getInstance() {
        return instance;
    }
    
    public Typeface getTypeface(){
    	return font;
    }
    /** 
     * 取得DaoMaster 
     *  
     * @param context 
     * @return 
     */  
    public DaoMaster getDaoMaster() {  
        if (daoMaster == null) {  
            OpenHelper helper = new DaoMaster.DevOpenHelper(getApplicationContext(),Constants.DB_NAME, null);  
            daoMaster = new DaoMaster(helper.getWritableDatabase());  
        }  
        return daoMaster;  
    }  
      
    /** 
     * 取得DaoSession 
     *  
     * @param context 
     * @return 
     */  
    public DaoSession getDaoSession() {  
        if (daoSession == null) {  
            if (daoMaster == null) {  
                daoMaster = getDaoMaster();  
            }  
            daoSession = daoMaster.newSession();  
        }  
        return daoSession;  
    } 
}
